package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.Fenlei;
import com.xiaomi.model.Result;
import com.xiaomi.service.FenLeiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName ShopSpuController
 * @Description TODO
 * @Author yp
 * @Date 2021/11/28 10:08
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/xiaomi", produces = "application/json; charset=utf-8")
public class FenLeiController {
    @Autowired
    private FenLeiService fenLeiService;

    @GetMapping("/fenlei/search.do")
    @ResponseBody
    public String query(){
        List<Fenlei> typeList = fenLeiService.query();
        //封装
        Result result = new Result();
        result.setCode(0);
        result.setData(typeList);
        result.setMsg("查询成功");

        String json = JSON.toJSONString(result);
        return json;
    }

    @GetMapping("/add.do")
    @ResponseBody
    public String jia(@RequestParam String label,@RequestParam Integer father){
        Fenlei fenlei = new Fenlei();
        fenlei.setFenLeiName(label);
        fenlei.setFather(father);
        fenLeiService.insert(fenlei);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("添加成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping("/update.do")
    @ResponseBody
    public String xiugai(Fenlei fenlei){
        fenLeiService.update(fenlei);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("修改成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping("/del.do")
    @ResponseBody
    public String delete(@RequestParam Integer id){
        fenLeiService.delete(id);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("删除成功");
        String json = JSON.toJSONString(result);
        return json;
    }
}
